package com.sikaryofficial.backend.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.sikaryofficial.backend.domain.dto.resp.productactivity.ActivityDTO;
import com.sikaryofficial.backend.domain.dto.resp.productactivity.ActivityReserveDTO;
import com.sikaryofficial.backend.domain.entity.ActivityReserve;
import com.sikaryofficial.backend.mapper.ActivityReserveMapper;
import com.sikaryofficial.backend.service.IActivityReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 活动预约Service业务层处理
 *
 * @author qinjinyuan
 * @date 2023-11-02
 */
@Service
public class ActivityReserveServiceImpl extends ServiceImpl<ActivityReserveMapper, ActivityReserve> implements IActivityReserveService {
    @Autowired
    private ActivityReserveMapper activityReserveMapper;

    /**
     * 查询活动预约状态
     *
     * @param activityIds 活动id
     * @param userId      用户ID
     * @return
     */
    @Override
    public Map<Long, Integer> getReserveStatus(List<Long> activityIds, Long userId) {
        if (activityIds == null || activityIds.isEmpty() || userId == null) {
            return Collections.emptyMap();
        }
        List<ActivityReserve> activityReserves = this.list(new LambdaQueryWrapper<ActivityReserve>()
                .select(ActivityReserve::getActivityId, ActivityReserve::getReserveStatus)
                .in(ActivityReserve::getActivityId, activityIds)
                .eq(ActivityReserve::getCreatedBy, userId)
                .eq(ActivityReserve::getDeletedVersion, 0L)
        );
        if (CollUtil.isEmpty(activityReserves)) {
            return Maps.newHashMap();
        }
        // key = activityId; value = reserveStatus
        return activityReserves.stream()
                .filter(item -> Objects.nonNull(item.getReserveStatus()))
                .collect(Collectors.toMap(ActivityReserve::getActivityId, ActivityReserve::getReserveStatus, (k1, k2) -> k1));
    }

    /**
     * 查询活动预约列表
     *
     * @param reserveStatus
     * @return 活动预约集合
     */
    @Override
    public IPage<ActivityReserveDTO> reserveList(Page<ActivityReserveDTO> customerPage, Long userId, Integer reserveStatus) {
        IPage<ActivityReserveDTO> activityReserveDTOIPage = activityReserveMapper.reserveList(customerPage, userId, reserveStatus);
        if (Objects.isNull(activityReserveDTOIPage) || CollUtil.isEmpty(activityReserveDTOIPage.getRecords())) {
            return new Page<>(1, 7, 0);
        }
        return activityReserveDTOIPage;
    }

    /**
     * 查询活动中奖列表
     *
     * @param customerPage
     * @param userId
     * @return
     */
    @Override
    public IPage<ActivityReserveDTO> awardsList(Page<ActivityReserveDTO> customerPage, Long userId) {
        return activityReserveMapper.awardsList(customerPage, userId);
    }

    /**
     * 根据活动ID，获取预约数量
     *
     * @param activityIds
     * @return
     */
    @Override
    public Map<Long, Integer> getReserveCountList(List<Long> activityIds) {
        if (CollUtil.isEmpty(activityIds)) {
            return Maps.newHashMap();
        }
        Map<Long, Integer> resultMap =  Maps.newHashMap();
        List<ActivityDTO> resultList = activityReserveMapper.getReserveCountList(activityIds);
        for (ActivityDTO item : resultList) {
            resultMap.put(item.getActivityId(), item.getReserveNum());
        }
        return resultMap;
    }
}
